﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Domain.Models;
using Domain.NHibernate.Models;
using Ninject;
using Web.Logic.Models.UnitOfWorkClasses;

namespace Web.Logic.Models
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel kernel;

        public NinjectControllerFactory()
        {
            kernel = new StandardKernel();

            AddBindings();
        }

        private void AddBindings()
        {
            kernel
                .Bind<IUnitOfWorkFactory>()
                .To<UnitOfWorkFactory>();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
                       ? null
                       : (IController) kernel.Get(controllerType);
        }
    }
}